/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.logging.Level;

import model.Preference;
import util.LoggingUtils;
import util.PlayerUtilities;

/**
 * 
 * @author Himadri
 */
public class HttpPostConnector {

	private String	result	= null;
	private URL		url		= null;
	private String	data;
	private boolean dmbunoapp = true;
	
	private static final String className = HttpPostConnector.class.getName();
	
	public static String getBoxToken() {
		return "menuboxid=" + Preference.getInstance().getBoxID()+"&requestFrom=unoappplayer" + "&updatetoken="
				+ model.Settings.UPDATE_CHECKER_URL_TOKEN;
	}

	public HttpPostConnector(URL url, String data) {
		this.url = url;
		this.data = data;
	}

	public boolean post() {
		
		final String classMethod = className + ".post()";
		LoggingUtils.logStart(classMethod);
		
		if (!PlayerUtilities.hasNetworkConnection()) {
			LoggingUtils.log(Level.SEVERE, "NO Network Connection to DMB Menubox Server "+classMethod);
			return false; // if no network, do nothing
		}

		boolean posted = false;
		HttpURLConnection conn = null;
		OutputStreamWriter wr = null;
		BufferedReader rd = null;
		try {
			conn = (HttpURLConnection) this.url.openConnection();
			conn.setDoOutput(true);
			conn.setConnectTimeout(20000);
			wr = new OutputStreamWriter(conn.getOutputStream());
			try {
				// Send data
				wr.write(this.data);
				wr.flush();

				// Get the response
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
					try {
						// System.out.println(conn.getResponseCode() + " " +
						// conn.getResponseMessage());
						String line;
						StringBuilder sb = new StringBuilder();
						while ((line = rd.readLine()) != null) {
							sb.append(line);
						}
							
						this.result = sb.toString();
						//System.out.println(this.url.toString()+"?"+data+" => " + this.result);
						
						posted = true;
					} finally {
						rd.close();
					}
				} else {
					//System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage());
				}
			} 
			catch(Exception e)
			{
				LoggingUtils.log(Level.SEVERE, e.getMessage() + classMethod,e);
			}
			finally 
			{
				wr.close();
				conn.disconnect();
			}
			
		}
		catch(UnknownHostException e)
		{
			LoggingUtils.log(Level.SEVERE, e.getMessage() + classMethod,e);
		}
		catch(ConnectException e)
		{
			LoggingUtils.log(Level.SEVERE, e.getMessage() + classMethod,e);
		}
		catch(SocketTimeoutException e)
		{
			LoggingUtils.log(Level.SEVERE, e.getMessage() + classMethod,e);
		}
		catch (IOException e) 
		{
			LoggingUtils.log(Level.SEVERE, e.getMessage() + classMethod,e);
		}
		catch (Exception e) 
		{
			LoggingUtils.log(Level.SEVERE, e.getMessage() + classMethod,e);
		}
		catch(Error er)
		{
			LoggingUtils.log(Level.SEVERE, er.getMessage() + classMethod,er);
		}
		finally
		{
			conn.disconnect();
		}
		return posted;
	}

	public synchronized String getResult() {
		return this.result;
	}
}